package ca.etsmtl.gti785.servlet;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.json.JSONObject;
import ca.etsmtl.gti785.servlet.Interfaces.*;
import ca.etsmtl.gti785.util.Constants;
import ca.etsmtl.gti785.util.Infos;

/**
 * This class is used to get the infos of the server and send it to the mobile client.
 * @see Infos
 * @author Étienne Faucher
 */
@Path("serverInfos")
public class ServerInfos implements IServerInfos 
{
	@Override
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("notifications")
	@Deprecated
	public void getNotification(@Suspended final AsyncResponse asyncResponse) 
	{
	}

	@Override
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("info")
	public String getInfo() 
	{
		double latitude = Constants.Server_Location_Default;
		double longitude = Constants.Server_Location_Default;
		
		try
		{
			URL whatismyip = new URL("http://checkip.amazonaws.com");
			BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));

			String ip = in.readLine();
			in.close();
			
			URL location = new URL("http://freegeoip.net/json/" + ip);
	        URLConnection yc = location.openConnection();
	        in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
	        StringBuilder json = new StringBuilder();
	        String readLine = null;
	        while ((readLine = in.readLine()) != null) 
	        {
	        	json.append(readLine);
	        }
	        in.close();

	        JSONObject obj = new JSONObject(json.toString());
	        latitude = obj.getDouble("latitude");
	        longitude = obj.getDouble("longitude");
	        
	        System.out.println("Latitude: " + latitude);
	        System.out.println("Longitude: " + longitude);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		String serverName = "Default server name";
		try 
		{
			serverName = InetAddress.getLocalHost().getHostName();
		} 
		catch (UnknownHostException e) 
		{
			e.printStackTrace();
		}
		
		Infos infos = new Infos(String.valueOf(longitude), String.valueOf(latitude), serverName, new Date());
		return infos.toJSON();
	}

	@Override
	@GET
	@Produces(MediaType.TEXT_HTML)
	@Path("QRCode")
	public String getQRCode(@Context ServletContext context) 
	{
		try
		{
			String ip = InetAddress.getLocalHost().getHostAddress();
			
			StringBuilder builder = new StringBuilder();
			builder.append("<html>");
			builder.append("<html>");
			builder.append("<head>");
			builder.append("<meta charset=\"UTF-8\">");
			builder.append("<title>Server's QRCode</title>");
			builder.append("</head>");
			builder.append("<body>");
			builder.append("<h1>Here's the QRCode of the server</h1>");
			
			builder.append("<iframe ");
			builder.append("Height=\"100%\" ");
			builder.append("Width=\"100%\" ");
			builder.append("Scrolling=\"no\"");
			builder.append("Frameborder=\"0\"");
			builder.append("Marginwidth=\"200\"");
			builder.append("Marginheight=\"40\"");
			builder.append("src=\"");
			builder.append("http://chart.apis.google.com/chart?cht=qr&chs=350x350&chld=L&choe=UTF-8&chl=");
			builder.append(String.format(Constants.Server_Address,ip));
			builder.append("\">");
			builder.append("</iframe>");
			builder.append("</body>");
			builder.append("</html>");
			return builder.toString();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return "";
	}
}
